package com.sprucetec.live.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sprucetec.live.common.dto.UserDto;
import com.sprucetec.live.common.util.HttpClient;
import com.sprucetec.live.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 **/
@Service("userService")
@Slf4j
public class UserServiceImpl implements UserService {

    @Value(value = "${mdms.serverurl}")
    private String mdmsServerUrl;

    @Value(value = "${mdms.queryUserByInputParam}")
    private String queryUserByInput;

    @Value(value = "${acms.serverurl}")
    private String acmsServerurl;

    @Value(value = "${acms.queryUserByUserCode}")
    private String queryUserByUserCode;

    @Value(value = "${acms.fuzzyQueryUser}")
    private String fuzzyQueryUser;

    @Override
    public UserDto queryUserByInput(String input) {
        Map<String, Object> param = new HashMap<>();
        param.put("code",input);
        param.put("psnStatus", 1);//在职
        String returnMsg = HttpClient.postJson(acmsServerurl + queryUserByUserCode,JSONObject.toJSONString(param));
        log.info("query ams user info param = {}, result = {}", param, returnMsg);
        JSONObject object = JSONObject.parseObject(returnMsg);
        UserDto user = new UserDto();
        if (!object.getString("replyDesc").equals("success")) {
            return null;
        }
        JSONObject ob = object.getJSONObject("data");
        user.setUserCode(ob.getString("code"));
        user.setFullUserName(ob.getString("name")+"-"+ob.getString("code"));
        user.setUserName(ob.getString("name"));
        user.setUserEmail(ob.getString("email"));
        user.setOrganName(ob.getString("org"));
        user.setOrganCode(ob.getString("pkOrg"));
        user.setDepartName(ob.getString("dept"));
        user.setDepartCode(ob.getString("pkDept"));
        return user;
    }

    @Override
    public List<Map<String, String>> fuzzyQueryUserList(String input) {

        Map<String, Object> param = new HashMap<>(1);
        param.put("inputValue", input);
        param.put("psnStatus", 1);//在职
        String returnMsg = HttpClient.postJson(acmsServerurl + fuzzyQueryUser, JSONObject.toJSONString(param));
        log.info("query ams user info param = {}, result = {}", param, returnMsg);
        JSONObject object = JSONObject.parseObject(returnMsg);
        if (!object.getString("replyDesc").equals("success")) {
            return null;
        }
        List<Map<String, String>> list = new ArrayList<>();
        JSONArray jsonArr = object.getJSONArray("data");
        int status = object.getIntValue("psnStatus");
        Iterator<Object> it = jsonArr.iterator();
        while (it.hasNext()) {
            JSONObject ob = (JSONObject) it.next();
            Map<String, String> map = new HashMap<>(8);
            map.put("userCode", ob.getString("code"));
            map.put("fullUserName", ob.getString("name")+"-"+ob.getString("code"));
            map.put("userName", ob.getString("name"));
            map.put("userEmail", ob.getString("email"));
            map.put("organName", ob.getString("org"));
            map.put("organCode", ob.getString("pkorg"));
            map.put("departName", ob.getString("dept"));
            map.put("departCode", ob.getString("pkdept"));
            list.add(map);
        }
        return list;
    }
}
